package com.cxrj.zhsq.administrate.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.administrate.dao.PersonnelMapper;
import com.cxrj.zhsq.administrate.dao.PropertyMapper;
import com.cxrj.zhsq.administrate.dao.VehicleMapper;
import com.cxrj.zhsq.administrate.entity.Personnel;
import com.cxrj.zhsq.administrate.entity.Property;
import com.cxrj.zhsq.administrate.entity.Vehicle;
import com.cxrj.zhsq.administrate.service.AdministrateService;
import com.cxrj.zhsq.userManage.dao.AppUserMapper;
import com.cxrj.zhsq.userManage.entity.AppUser;

@Service
public class AdministrateServiceImpl implements AdministrateService {

	@Resource
	private PersonnelMapper personnelMapper;

	@Resource
	private PropertyMapper propertyMapper;

	@Resource
	private VehicleMapper vehicleMapper;

	@Resource
	private AppUserMapper appUserMapper;

	@Override
	public Map<String, Object> selectPersonnelPager(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		// 总记录数
		String recordsTotal = "0";

		sqlMap.put("name", request.getParameter("name"));
		sqlMap.put("card", request.getParameter("card"));
		sqlMap.put("tel", request.getParameter("tel"));
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));

		// 获取数据库总记录数
		recordsTotal = personnelMapper.getPersonnelCount(sqlMap) + "";

		List<Map<String, Object>> list = personnelMapper.selectPersonnelPager(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> selectPropertyPager(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		// 总记录数
		String recordsTotal = "0";

		sqlMap.put("building", request.getParameter("building"));
		sqlMap.put("unit", request.getParameter("unit"));
		sqlMap.put("doorCard", request.getParameter("doorCard"));
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));

		// 获取数据库总记录数
		recordsTotal = propertyMapper.getPropertyCount(sqlMap) + "";

		List<Map<String, Object>> list = propertyMapper.selectPropertyPager(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> selectVehiclePager(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		// 总记录数
		String recordsTotal = "0";

		sqlMap.put("plateNumber", request.getParameter("plateNumber"));
		sqlMap.put("user", request.getParameter("user"));
		sqlMap.put("idCard", request.getParameter("idCard"));
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));

		// 获取数据库总记录数
		recordsTotal = vehicleMapper.getVehicleCount(sqlMap) + "";

		List<Map<String, Object>> list = vehicleMapper.selectVehiclePager(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> selectPersonnelById(String id, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Personnel personnel = personnelMapper.selectByPrimaryKey(id);
		resultMap.put("personnel", personnel);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectPropertyById(String id, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Property property = propertyMapper.selectByPrimaryKey(id);
		resultMap.put("property", property);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectVehicleById(String id, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Vehicle vehicle = vehicleMapper.selectByPrimaryKey(id);
		resultMap.put("vehicle", vehicle);
		return resultMap;
	}

	@Override
	public Map<String, Object> insertAndUpdatePersonnel(Personnel personnel) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = 0;
		if (personnel.getId().equals("")) {
			personnel.setId(UUID.randomUUID().toString());
			personnel.setCreateTime(new Date());
			personnel.setStatus("1");
			c = personnelMapper.insertSelective(personnel);
		} else {
			c = personnelMapper.updateByPrimaryKeySelective(personnel);
		}
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> insertAndUpdateProperty(Property property) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = 0;
		if (property.getId().equals("")) {
			property.setId(UUID.randomUUID().toString());
			property.setCreateTime(new Date());
			property.setStatus("1");
			c = propertyMapper.insertSelective(property);
		} else {
			c = propertyMapper.updateByPrimaryKeySelective(property);
		}
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> insertAndUpdateVehicle(Vehicle vehicle) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = 0;
		if (vehicle.getId().equals("")) {
			vehicle.setId(UUID.randomUUID().toString());
			vehicle.setCreateTime(new Date());
			vehicle.setStatus("1");
			c = vehicleMapper.insertSelective(vehicle);
		} else {
			c = vehicleMapper.updateByPrimaryKeySelective(vehicle);
		}
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> insertAppUser(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		Personnel personnel = personnelMapper.selectByPrimaryKey(id);
		sqlMap.put("name", personnel.getNickname());
		sqlMap.put("tel", personnel.getTel());
		sqlMap.put("idCard", personnel.getIdCard());
		if (appUserMapper.selectByMap(sqlMap) != 0) {
			resultMap.put("result", 2);
			return resultMap;
		}
		List<Property> list = propertyMapper.selectByPersonnel(sqlMap);
		if (list.size() == 0) {
			resultMap.put("result", 3);
			return resultMap;
		}
		AppUser appUser = new AppUser();
		appUser.setUserId(UUID.randomUUID().toString());
		appUser.setUserAccount(personnel.getTel());
		appUser.setUserPassword("123456");
		appUser.setUserStatus("1");
		appUser.setUserName(personnel.getNickname());
		appUser.setUserIndentity(personnel.getIdentity());
		appUser.setUserSex(personnel.getS1());
		appUser.setUserMobile(personnel.getTel());
		appUser.setCommunityName(personnel.getPost());
		appUser.setUserIdcard(personnel.getIdCard());
		String address = "";
		for (Property property : list) {
			address += "," + property.getBuilding() + "-" + property.getUnit() + "-" + property.getDoorCard();
		}
		if (!address.equals("")) {
			appUser.setUserAddress(address.substring(1));
		}
		Integer c = appUserMapper.insertSelective(appUser);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}
}
